from flask import Flask, request, jsonify

app = Flask(__name__)


# 模拟的文件生成处理
def process_file_generation(messages):
    # 模拟生成 taskId 和 fileKey
    taskId = "12345"
    fileKey = "generated-file-key"

    # 获取文件名
    file_name = messages[0].get('fileDTOList', [{}])[0].get('name', 'generated_file.docx')

    # 模拟生成的文档名
    content = "已生成防喷器的计算书"

    return taskId, file_name, fileKey, content


# 接收 POST 请求的端点
@app.route('/file/upload', methods=['POST'])
def handle_request():
    try:
        # 从请求体中解析JSON数据
        data = request.get_json()

        # 获取 modelType 和 messages
        modelType = data.get('modelType', 'DOC')
        messages = data.get('messages', [])

        if not messages:
            return jsonify({"error": "No messages found"}), 400

        # 调用处理函数来生成任务
        taskId, file_name, fileKey, content = process_file_generation(messages)

        # 构造响应体
        response = {
            "taskId": taskId,
            "modelType": modelType,
            "fileDTOList": [{
                "name": file_name,
                "fileKey": fileKey,
                "type": "docx"
            }],
            "content": content
        }

        # 返回响应
        return jsonify(response), 200

    except Exception as e:
        return jsonify({"error": str(e)}), 500


if __name__ == '__main__':
    app.run(debug=True)
